//
//  XiangQuToastManager.m
//  XiangQu
//
//  Created by yandi on 14/10/29.
//  Copyright (c) 2014年 Qiuyin. All rights reserved.
//

#import "LBToastManager.h"
#import "AppDelegate.h"

#define minshowtime   .5
#define progesswidth  5.
#define rotationspeed 2
#define customcenter  CGPointMake(22.5, 22.5)
@interface XiangQuAnnurImageView :UIImageView {
    @public
    UIBezierPath *trackPath;
    CAShapeLayer *trackLayer;
    UIBezierPath *progressPath;
    CAShapeLayer *progressLayer;
}
@end

@implementation XiangQuAnnurImageView

#pragma mark -initWithFrame
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self actionRenderLayerComponents];
    }
    return self;
}

#pragma mark -actionRenderLayerComponents
- (void)actionRenderLayerComponents {
    
    trackLayer = [CAShapeLayer layer];
    trackPath = [UIBezierPath bezierPathWithArcCenter:self.center
                                               radius:(CGRectGetWidth(self.bounds)-progesswidth)/2. startAngle:0
                                             endAngle:M_PI*2 clockwise:YES];
    trackLayer.frame = self.bounds;
    trackLayer.path = trackPath.CGPath;
    trackLayer.lineWidth = progesswidth;
    trackLayer.strokeColor = [UIColor whiteColor].CGColor;
    [self.layer addSublayer:trackLayer];
    
    progressPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(22.5, 22.5) radius:(CGRectGetWidth(self.bounds)-progesswidth)/2. startAngle:-M_PI_2 endAngle:M_PI/3 clockwise:YES];
    progressLayer = [CAShapeLayer layer];
    progressLayer.frame = self.bounds;
    progressLayer.lineWidth = progesswidth;
    progressLayer.lineCap = kCALineCapRound;
    progressLayer.path = progressPath.CGPath;
    progressLayer.strokeColor = RGB(100, 100, 87).CGColor;
    [self.layer addSublayer:progressLayer];
}

#pragma mark -actionRotationAnimation
- (void)actionRotationAnimation:(CGFloat)speed {
    [progressLayer removeAllAnimations];
    
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI*2];
    rotationAnimation.duration = speed;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = MAXFLOAT;
    [progressLayer addAnimation:rotationAnimation forKey:@"rotationAnimation1"];
}
@end


@interface LBToastManager () {
    NSTimer *progresstimer;
    MBProgressHUD *toastHud;
    MBProgressHUD *progressHud;
    XiangQuAnnurImageView *annurImageView;
}
@end

static LBToastManager *toastManager;
@implementation LBToastManager

#pragma mark -manager
+ (LBToastManager *)manager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        toastManager = [[LBToastManager alloc] init];
        [toastManager actionRenderUIComponents];
    });
    
    return toastManager;
}

#pragma mark -keyWindow
+ (UIWindow *)keyWindow {
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    UIWindow *keywindow = delegate.window;
    
    return keywindow;
}

#pragma mark -actionRenderUIComponents
- (void)actionRenderUIComponents {
    UIWindow *keywindow = [LBToastManager keyWindow];
    
    toastHud = [[MBProgressHUD alloc] initWithWindow:keywindow];
    toastHud.userInteractionEnabled = NO;
    toastHud.mode = MBProgressHUDModeText;
    toastHud.minShowTime = minshowtime*2;
    [keywindow addSubview:toastHud];
    
    progressHud = [[MBProgressHUD alloc] initWithWindow:keywindow];
    progressHud.animationType = MBProgressHUDAnimationFade;
//    progressHud.mode = MBProgressHUDModeCustomView;
    progressHud.userInteractionEnabled = NO;
    progressHud.minShowTime = minshowtime;
    progressHud.labelFont = FONT(12.f);
//    progressHud.labelText = @"加载中...";
    progressHud.square = YES;
//    annurImageView = [[XiangQuAnnurImageView alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
//    progressHud.customView = annurImageView;
    [keywindow addSubview:progressHud];
}

#pragma mark -toast
- (void)showtoast:(NSString *)toastStr {
    if (![TSTools isEmpty:toastStr]) {
        if (toastStr.length > 15) {
            toastHud.labelText = @"";
            toastHud.detailsLabelText = toastStr;
        } else {
            toastHud.labelText = toastStr;
            toastHud.detailsLabelText = @"";
        }
        [[LBToastManager keyWindow] bringSubviewToFront:toastHud];
        [toastHud show:YES];
        [toastHud hide:YES];
    }
}

#pragma mark -progress
- (void)hideprogress {
    [progressHud hide:YES];
}

- (void)showprogress:(NSString *)text {
    if (text && ![text isEqualToString:@""]) {
        [progressHud setLabelText:text];
    }
    
    [[LBToastManager keyWindow] bringSubviewToFront:progressHud];
    [toastManager actionCountProgress];
    [progressHud show:YES];
}

/* 设置progress name */
- (void)setProgressText:(NSString *)text
{
    if (progressHud) {
        [progressHud setLabelText:text];
    }
}

#pragma mark -actionRemoveAnimation
- (void)actionRemoveAnimation {
    [annurImageView->progressLayer removeAllAnimations];
}

#pragma mark -actionCountProgress
- (void)actionCountProgress {
    [annurImageView actionRotationAnimation:rotationspeed];
}
@end
